package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kaisa.activity.BaseApp;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.SortSearchAdapter;
import com.kaisa.activity.bean.SearchHistoryDb;
import com.kaisa.activity.bean.SortSearchBean;
import com.kaisa.activity.constant.UrlHttp;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class SortSearchActivity extends AppCompatActivity {
    @ViewInject(R.id.tv_sort_cancel)
    private TextView searchCancel;
    @ViewInject(R.id.gv_sort_gview)
    private GridView searchGridView;
    @ViewInject(R.id.lv_searchhistory)
    private ListView searchhistoryLV;
    @ViewInject(R.id.ll_searchhistory)
    private LinearLayout searchLL;

    private ArrayAdapter adapterLV;

    private SortSearchAdapter adapter;
    private List<SortSearchBean.HotTermsEntity> list = new ArrayList<SortSearchBean.HotTermsEntity>();

    private List<SearchHistoryDb> listdb = new ArrayList<SearchHistoryDb>();
    private List<String> listContry = new ArrayList<String>();

    private DbManager db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_search);
        x.view().inject(this);
        initView();
        initData();
        addListener();
    }

    private void initView() {
        adapter = new SortSearchAdapter(list, this);
        searchGridView.setAdapter(adapter);

        adapterLV = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listContry);
        searchhistoryLV.setAdapter(adapterLV);

        db = BaseApp.getDB();

        seeList();

    }

    private void initData() {
        getParams();
        x.http().get(getParams(), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                SortSearchBean searchBean = JSONObject.parseObject(s, SortSearchBean.class);
                list.addAll(searchBean.getHotTerms());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void addListener() {
        //取消点击事件
        searchCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        searchhistoryLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String queryKey = listContry.get(i);
                Intent intent = new Intent(getApplication(), SearchCountriesActivity.class);
                intent.putExtra("queryKey", queryKey);
                startActivity(intent);
            }
        });
    }

    public void click(View v) {
        if (v.getId() == R.id.btn_searchhistory) {
            try {
                db.delete(SearchHistoryDb.class);
                searchLL.setVisibility(View.GONE);
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
    }


    private void addList() {
        if (listdb != null && listdb.size()>0) {
            searchLL.setVisibility(View.VISIBLE);
        } else {
            searchLL.setVisibility(View.GONE);
        }


    }
    public void seeList() {

        try {
            listdb.clear();
            if (db.findAll(SearchHistoryDb.class) != null) {
                listdb.addAll(db.findAll(SearchHistoryDb.class));
                for (int i = 0; i < listdb.size(); i++) {
                    listContry.add(listdb.get(i).getName());
                }
                adapterLV.notifyDataSetChanged();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        addList();
    }

    public RequestParams getParams() {
        RequestParams params = new RequestParams(UrlHttp.PATH_SORT_SEARCH);
        params.addBodyParameter("account", "123");
        params.addBodyParameter("channel", "CS0001");
        params.addBodyParameter("channelSecond", "a0002");
        params.addBodyParameter("terminal", "android");
        params.addBodyParameter("sourceFlag", "860806020832282");
        return params;
    }
}
